package cst.a703xmq.zyfypt_703xmq.Fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a703xmq.zyfypt_703xmq.Activity.TcaseActivity;
import cst.a703xmq.zyfypt_703xmq.Adapter.TCaseAdapter;
import cst.a703xmq.zyfypt_703xmq.Bean.TcaseBean;
import cst.a703xmq.zyfypt_703xmq.Interface.XmqListener;
import cst.a703xmq.zyfypt_703xmq.Model.ResModel;
import cst.a703xmq.zyfypt_703xmq.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class CFragment_case extends BaseFragment {
  private RecyclerView recyclerView;
  private TCaseAdapter adapter;
  private List<TcaseBean> list;
  private RecyclerView.LayoutManager layoutManager;
  private XmqListener<List<TcaseBean>> case_listener;
  private ResModel model;
  private Context context;


  public CFragment_case() {
    // Required empty public constructor
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
    context = getActivity();

    View view = inflater.inflate(R.layout.cfragment_case, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.case_rec);
    list = new ArrayList<TcaseBean>();
    layoutManager = new LinearLayoutManager(context);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new TCaseAdapter(context);
    adapter.setOnItemClickListener(new TCaseAdapter.OnItemClickListener(){
      @Override
      public void onItemClick(View view, int position){
        Intent intent = new Intent(context, TcaseActivity.class);
        intent.putExtra("resid",list.get(position).getId());
        context.startActivity(intent);
      }

      @Override
      public void onItemLongClick(View view, int position){}
    });
    adapter.setList(list);
    recyclerView.setAdapter(adapter);
    case_listener = new XmqListener<List<TcaseBean>>() {
      @Override
      public void onResponse(List<TcaseBean> l) {
        list=l;
        adapter.setList(list);
      }

      @Override
      public void onFail(String msg) {
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();

      }
    };
//    model = new ResModel("gson",getSessionID());
    model = new ResModel("gson");

    model.getList("tcase", 1,case_listener);

    return view;
  }
}